package com.xxwy.hard.taskqueue;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * @author wy
 * @date 2020/3/5 9:34
 */
public class CommonHandler extends ChannelInboundHandlerAdapter {

  /**
   * 当业务处理有一个片段非常耗时，可以放入taskQueue异步执行,
   * @param ctx
   * @param msg
   * @throws Exception
   */
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ctx.channel().eventLoop().execute(()->{
      try {
        System.out.println(Thread.currentThread().getName());
        Thread.sleep(10 * 1000);
        ctx.channel().writeAndFlush(Unpooled.copiedBuffer("hi task in" , CharsetUtil.UTF_8));
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    });
    System.out.println("go on");
  }

  @Override
  public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
    ctx.channel().writeAndFlush(Unpooled.copiedBuffer("hi channelReadComplete" , CharsetUtil.UTF_8));
  }
}
